/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.table.person;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.swing.SingleFiledLayout;
import twaver.table.TElementTable;
import twaver.table.TPropertySheet;

class UnderlineBorder implements Border, Serializable {	
	int offset = 2;
    public static final UnderlineBorder INSTANCE = new UnderlineBorder();

    private UnderlineBorder() {}

    public void paintBorder(Component c,
                            Graphics g,
                            int x,
                            int y,
                            int width,
                            int height) {
        g.setColor(Color.gray);
        g.drawLine(x, y + height - offset, x + width, y + height - offset);
    }

    public Insets getBorderInsets(Component c) {
        return new Insets(0, 0, offset, 0);
    }

    public boolean isBorderOpaque() {
        return true;
    }
}

public class MultiLineRenderer extends JPanel implements TableCellRenderer{
	
	private JTable table = null;
	
	private static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
	private static Border focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
	private static Icon icon = TWaverUtil.getIcon("/resource/image/direction/e.png");
	
	private JLabel firstLabel = new JLabel();
	private JLabel lastLabel = new JLabel();
	private JLabel fullLabel = new JLabel();
	
	private JButton firstButton = new JButton(icon);
	private JButton lastButton = new JButton(icon);
	private JButton fullButton = new JButton(icon);
	
	public MultiLineRenderer(){
		this.setLayout(new SingleFiledLayout(SingleFiledLayout.COLUMN, SingleFiledLayout.FULL, 1));
		this.setOpaque(true);
		this.addLabel(firstLabel, firstButton);
		this.addLabel(lastLabel, lastButton);
		this.addLabel(fullLabel, fullButton);
	}
	
	private void addLabel(final JLabel label, final JButton button){
		button.setBorder(BorderFactory.createLineBorder(Color.gray));
		JPanel pane = new JPanel(new BorderLayout());
		pane.setBorder(TWaverConst.EMPTY_BORDER);
		pane.setBorder(UnderlineBorder.INSTANCE);
		pane.add(label, BorderLayout.CENTER);
		pane.add(button, BorderLayout.EAST);
		label.setOpaque(false);
		button.setOpaque(false);
		pane.setOpaque(false);
		this.add(pane);
		
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(
						button, 
						label.getText(), 
						"I am Button",
						JOptionPane.INFORMATION_MESSAGE);
				if(table != null){
					table.requestFocus();
				}
			}
		});
	}

	public Component getTableCellRendererComponent(
			JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		this.table = table;

		Person person = null;
		if(table instanceof TElementTable){
			person = (Person)((TElementTable)table).getElementByRowIndex(row); 
		}
		else if(table instanceof TPropertySheet){
			person = (Person)((TPropertySheet)table).getDataBox().getLastSelectedElement();
		}
		
		if(person != null){
			this.firstLabel.setText(person.getFirstName());
			this.lastLabel.setText(person.getLastName());
			this.fullLabel.setText(person.getFullName());
		}
		
		if(hasFocus){
    		this.setBorder(focusBorder);
    	}else{
    		this.setBorder(noFocusBorder);
    	}
		if(isSelected){
			this.setBackground(table.getSelectionBackground());
			firstLabel.setForeground(table.getSelectionForeground());
			lastLabel.setForeground(table.getSelectionForeground());
			fullLabel.setForeground(table.getSelectionForeground());
		}else{
			this.setBackground(table.getBackground());
			firstLabel.setForeground(table.getForeground());
			lastLabel.setForeground(table.getForeground());
			fullLabel.setForeground(table.getForeground());
		}
		return this;
	}
	

}